package com.zmc.member.infrastructure.component.shopping.impl;

import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.member.infrastructure.bean.query.ShoppingCartQuery;
import com.zmc.member.infrastructure.bean.vo.*;
import com.zmc.member.infrastructure.component.shopping.ShoppingCartStrategyService;
import com.zmc.member.infrastructure.entity.ShoppingCartItemEntity;
import com.zmc.member.infrastructure.service.ShoppingCartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：zmc
 * @Date：2025/5/27 14:04
 */
@Component
public class ShoppingPersistenceServiceImpl implements ShoppingCartStrategyService {

    @Autowired
    private ShoppingCartItemService shoppingCartItemService;

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public void merge(List<ShoppingCartVo> list) {
        List<ShoppingCartItemEntity> items = new ArrayList<>();
        list.forEach(vo -> {
            ShoppingCartItemEntity entity = new ShoppingCartItemEntity();
            entity.setSelected(vo.getSelected());
            entity.setSkuId(vo.getSkuId());
            entity.setMemberId(authTokenService.getMemberInfo().getMemberId());
            entity.setPurchaseQuantity(vo.getCount());
            items.add(entity);
        });
        shoppingCartItemService.saveOrUpdateItemList(items);
    }

    @Override
    public void delete(List<String> skuIds) {
        shoppingCartItemService.removeBySkuIds(skuIds);
    }

    @Override
    public void add(ShoppingCartVo shoppingCartVo) {
        shoppingCartItemService.addShoppingCart(shoppingCartVo);
    }

    @Override
    public void updateShoppingCartItemBySkuId(ShoppingCartItemUpdateCountVo vo) {
        shoppingCartItemService.updateShoppingCartItem(vo);
    }

    @Override
    public void setSelectedStatus(ShoppingCartSelectedVo vo) {
        shoppingCartItemService.setSelectedStatus(vo);
    }

    @Override
    public void clearHasSelected() {
        shoppingCartItemService.clearHasSelected();
    }

    @Override
    public List<ShoppingCartVo> getSelectedList(List<String> skuIds) {
        return shoppingCartItemService.getSelectedList(skuIds);
    }

    @Override
    public PageUtils shoppingCartList(ShoppingCartQuery cartQuery) {
        return shoppingCartItemService.shoppingCartList(cartQuery);
    }
}
